package com.feonix.common.task;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.feonix.common.datasource.DataSourceSwitch;

public abstract class BaseTask implements Job {

	protected Log logger = LogFactory.getLog(super.getClass());
	/**
	 * 任务唯一id
	 */
	String taskId;

	/**
	 * 任务名称
	 */
	String taskName;

	/**
	 * 任务组，默认为DEFAULT
	 */
	String taskGroup = "DEFAULT";

	/**
	 * 任务执行周期
	 */
	Object taskSchedule;

	/**
	 * 优先级
	 */
	int priority = 1;

	/**
	 * 是否现在启动
	 */
	boolean startNow = false;

	/**
	 * 首次启动时间
	 */
	Date startTime = null;

	/**
	 * 任务所带参数
	 */
	JobDataMap dataMap = new JobDataMap();

	public BaseTask() {
		setCanPause(false);
		setCanRemove(false);
		config();
		this.dataMap.put("JOB_FIRE_COUNT", 0);
		this.dataMap.put("JOB_FIRE_COUNT_ERROR", 0);
	}

	/**
	 * 设置任务的详细信息
	 */
	protected abstract void config();

	/**
	 * 任务业务执行
	 */
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
		int job_fire_count = jobDataMap.getInt("JOB_FIRE_COUNT");
		int job_fire_count_error = jobDataMap.getInt("JOB_FIRE_COUNT_ERROR");
		try {
			logger.debug("[GROUP:" + this.taskGroup + "]-[TASK:" + this.taskName + "] START.");
			doWork(jobDataMap);
			logger.debug("[GROUP:" + this.taskGroup + "]-[TASK:" + this.taskName + "] FINISH.");
		} catch (Exception exception) {
			logger.error("[GROUP:" + this.taskGroup + "]-[TASK:" + this.taskName + "] FAIL", exception);
			jobDataMap.put("JOB_FIRE_COUNT_ERROR", ++job_fire_count_error);
		} finally {
			jobDataMap.put("JOB_FIRE_COUNT", ++job_fire_count);
			DataSourceSwitch.changeDefault();
		}
	}

	protected abstract void doWork(JobDataMap paramJobDataMap);

	public void addParam(String paramString, Object paramObject) {
		this.dataMap.put(paramString, paramObject);
	}

	public void setCanPause(boolean paramBoolean) {
		this.dataMap.put("JOB_CAN_PAUSE", paramBoolean);
	}

	public void setCanRemove(boolean paramBoolean) {
		this.dataMap.put("JOB_CAN_REMOVE", paramBoolean);
	}

	public Log getLogger() {
		return logger;
	}

	public void setLogger(Log logger) {
		this.logger = logger;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public String getTaskGroup() {
		return taskGroup;
	}

	public void setTaskGroup(String taskGroup) {
		this.taskGroup = taskGroup;
	}

	public Object getTaskSchedule() {
		return taskSchedule;
	}

	public void setTaskSchedule(Object taskSchedule) {
		this.taskSchedule = taskSchedule;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public boolean isStartNow() {
		return startNow;
	}

	public void setStartNow(boolean startNow) {
		this.startNow = startNow;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public JobDataMap getDataMap() {
		return dataMap;
	}

	public void setDataMap(JobDataMap dataMap) {
		this.dataMap = dataMap;
	}

}
